package controller;


import pojo.User;
import service.UserService;
import view.AddUser;
import view.UserView;


import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddUserListener implements ActionListener {
    private AddUser addUser;
    private UserView userView;
    public AddUserListener(AddUser dialog, UserView userView){
        super();
        this.addUser=dialog;
        this.userView=userView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("返回")) {
            addUser.dispose();
        }
        if(e.getActionCommand().equals("新增")) {
            String userId = addUser.getTfNum().getText();
            String userName = addUser.getTfName().getText();
            String passWord = addUser.getTfPwd().getText();
            String gender =addUser.getTfGender().getText();
            String phoneNumber = addUser.getTfPN().getText();
            String type = (String) addUser.getCBType().getSelectedItem();
            User user = new User(userId, userName, passWord, gender, phoneNumber, type);
            UserService userService = new UserService();
            userService.add(user);
            int a = JOptionPane.showConfirmDialog(addUser, "确认新增?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                userView.refresh();
                addUser.dispose();

            }
        }
    }
}
